
layui.define(['form', 'layedit', 'laydate','formSelects'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let formSelects = layui.formSelects;
    let index;

    zmnLayUtil.layFormVerify(form);

    var data = new Array();
    var oneData = new Object();
    let initData = [1, 7, 30, 60, 90, 120, 150, 180, 270, 365, 730, 1095, 1825];
    for (var i = 0; i < initData.length; i++) {
        var oneData = new Object();
        oneData['name'] = initData[i];
        oneData['value'] = initData[i];
        data.push(oneData);
    }

    //初始化
    formSelects.data('warrantDuration', 'local', {
        arr: data
    });

    doRenderDiv(json.obj.ruleId);

    if (json.obj.warrantyDurationStr != null && json.obj.warrantyDurationStr != '') {
        let length = json.obj.warrantyDurationStr.length;
        let str = json.obj.warrantyDurationStr.substr( 1, length - 2);
        let array = str.split("$");
        formSelects.value('warrantDuration', array);
    }

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;

        let warrantyContentArr = new Array();
        let node = $('input[name="warrantyContent"]');
        for(let k in node){
            if(node[k].checked)
                warrantyContentArr.push(node[k].value);
        }

        let warrantDuration = formSelects.value('warrantDuration', 'val');
        if (!(obj.field.ruleId == null || obj.field.ruleId == 1)) {
            if(warrantyContentArr.length == 0){
                layer.msg("保修内容不能为空", {icon: 2});
                return false;
            }
            data.warrantyContentIdStr = '$'+warrantyContentArr.join("$")+'$';

            if (warrantDuration == undefined || warrantDuration == null || warrantDuration.length == 0) {
                layer.msg("保修时长不能为空", {icon: 2});
                return false;
            }

            if (zmnUtil.isEmptyNull(data.extendType)) {
                layer.msg("非不保修规则下延长保修必填", {icon: 2});
                return false;
            }

            data.warrantyDurationStr = '$'+warrantDuration.join("$")+'$';
        } else {
            data.warrantyContentIdStr = "";
            data.warrantyDurationStr = "";
            data.extendType = "";
            data.extendedDuration = "";
        }

        if (!zmnUtil.isEmptyNull(data.extendType) && data.extendType == 2) {
            if (zmnUtil.isEmptyNull(data.extendedDuration)) {
                layer.msg("延长保修时，延长时长必填", {icon: 2});
                return false;
            }

            if (!/^[0-9]+$/.test(data.extendedDuration)) {
                layer.msg("延长时长只能是正整数", {icon: 2});
                return false;
            }
        }

        let url = "/service/item/addEditWarrantyInfo.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                layer.close(index);
                layer.msg(json.message);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete: function () {
                layer.close(index);
            }
        });

        return false;
    });

    form.on('radio(ruleId)', function (data) {
        let value = data.value;
        doRenderDiv(value);
    });

    form.on('radio(extendType)', function (data) {
        // 如果不延长保修则隐藏延长时长输入框
        if (data.value == 1) {
            $("input[name=extendedDuration]").val("");
            $("#extendedDurationDiv").hide();
        } else {
            $("#extendedDurationDiv").show();
        }

        form.render(null, "zmn-form");
    });

    // 选择不保修时，隐藏保修内容和保修时长
    function doRenderDiv(value){
        if (value == null || value == 1) {
            $("#warrantyContent").hide();
            $("#warrantDuration").hide();
            formSelects.value('warrantDuration', []);
            let node = $('input[name="warrantyContent"]');
            for(var i = 0; i < node.length; i++){
                $(node[i]).prop("checked",false);
            }

            $("#extendAllDiv").hide();
        } else {
            $("#warrantyContent").show();
            $("#warrantDuration").show();

            $("#extendAllDiv").show();
            let currentExtendTypeValue = $("input[name='extendType']:checked").val();
            if (currentExtendTypeValue == 2) {
                $("#extendedDurationDiv").show();
            } else {
                $("#extendedDurationDiv").hide();
            }
        }
        form.render(null, "zmn-form");
    }

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('serv_service_item_warranty_info_detail', {});
});

function cancel() {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}